

<div class="layui-btn-group enian-btn-group">
	<button id='SonApplist_add' class="layui-btn layui-btn-sm layui-btn-primary">
  		<i class="layui-icon">&#xe61f;</i><div class="enian-btn-group-text">添加子应用</div>
  	</button>
  	<button id='search' class="layui-btn layui-btn-sm layui-btn-primary">
  		<i class="layui-icon">&#xe615;</i><div class="enian-btn-group-text">搜索</div>
  	</button>
</div>		 

<div id="data">
	
</div>

<script type="text/html" id="SonApplist_typeTpl">
  {{# if(d.type==0){  }}
  		<div style="color: gray;">普通页面</div>
  {{# }else if(d.type==1){ }}
  		数据表模板
  {{# }else if(d.type==2){ }}
  		API
  {{# } }}
</script>

<script type="text/html" id="SonApplist_is_displayTpl">
	  {{# if(d.type==2){  }}
  		<input type="checkbox" name="aaa" lay-skin="switch" lay-text='禁止|禁止' disabled>
	  {{# }else{ }}
	  	<input type="checkbox" name="is_display" value="{{d.sid}}" lay-skin="switch" lay-text="" lay-filter="SonApplist_is_display" {{ d.is_display == 1 ? 'checked' : '' }}>
	  {{# } }}
	
</script>


<script type="text/html" id="SonApplist_s_tool">
  {{# if(d.type==1){  }}
  		<a class="layui-btn layui-btn-xs" lay-event="SonApplist_power">设置</a>
  {{# }else{ }}
  		<!--<a class="layui-btn layui-btn-xs" lay-event="SonApplist_power">编辑</a>-->
  {{# } }}
  
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="SonApplist_del">删除</a>
</script>
  
<script type="text/html" id="update-type-tpl">
	<div class="layui-form" align="center" style="margin: 0 10px;margin-top:5px;" lay-filter="update-type-tpl-form" >
		<select name="type" class="update-type-tpl-form-type" data-id="{{d.id}}" lay-filter="update-type-tpl-form-type">
		    <option value="0">普通页面</option>
		    <!-- <option value="1">数据表模板</option> -->
		    <option value="2">API接口</option>
		    
		</select>

	</div>

	
</script>        

<script>
layui.use(['table','form','jquery','enianTable','laytpl'], function() {
	var table = layui.table
	,enianTable = layui.enianTable
	,laypage = layui.laypage
	,$ = layui.jquery
	,laytpl=layui.laytpl
	,form = layui.form;

	var tableArg={where:[],order:[]};
	
	var fieldData = [{tableHead:{field:'sid', width:80, title: 'ID', sort: true},type:false,notRender:['search']},
	{tableHead:{field:'order',  title: '排序',sort: true,width:80,edit: 'text'},type:'input'},
	{tableHead:{field:'name', width:100, title: '名称',edit: 'text'},type:'input'},
	//{tableHead:{field:'icon', width:100, title: '图标',edit: 'text'},type:'input',typeData:{}},
	{tableHead:{field:'url', width:150,event: 'url',title: '页面地址（节点）'},type:'input',typeData:{}},
	{tableHead:{field:'is_display', width:100, title: '是否展示',align:'center',unresize: true,toolbar: '#SonApplist_is_displayTpl'},type:'switch',typeData:{idName:'sid',on:{data:1,name:''},off:{data:0,name:''}}},
	{tableHead:{field:'is_auth', width:100,align:'center', title: '加入权限'},type:'switch',typeData:{idName:'sid',on:{data:1,name:''},off:{data:0,name:''}}},
	{tableHead:{field:'type', width:100,align:'center',event: 'type', title: '类型',templet: '#SonApplist_typeTpl'},type:'input',typeData:{}},
	{tableHead:{field:'note', width:150, title: '备注',edit: 'text'},type:'input',typeData:{}},
	{tableHead:{field:'note2', width:100, title: '备注2',edit: 'text'},type:'input',typeData:{}},
	{tableHead:{field:'tool', width:130 ,align:'right',fixed: 'right',toolbar: '#SonApplist_s_tool'},type:false}
	]
	
    layui.link('/static/layui_extends/enianTable/enianTable.css');
    var ets = enianTable.search(fieldData);
    $('#search').click(function(){
		ets.open();
    });
    
    //监听搜索
	ets.submit(function(d){
		// console.log(d);
		getTable.renderObj().reload({
		  where:{where:d}
		  ,page: {
		    curr: 1 //重新从第 1 页开始
		  }
		});
	});
	
	 var nav_bar=[
				{"type":"menu","url":"/index/applist.html"},//或者{"type":"menu","url":"/admin/setting..."}
//				{"type":"url","url":"","title":"大上一页"},
				{"type":"url","url":"{:url('applist')}","title":enianAdmin.url().params.appname},
				{"type":"url","url":"","title":"子应用"},
			]
	enianAdmin.render_nav_bar(nav_bar);
	$('#SonApplist_add').click(function(){
		enianAdmin.go('{:url("addSonApp")}?appid={:input("param.appid")}&appname='+enianAdmin.url().params.appname);
		//location.href='{:url("addSonApp")}?appid={:input("param.appid")}';
    });
    
    //渲染表格
    var getTable = enianTable.table({
		elem:"#data"
		,url:"{:url('',['t'=>1])}?appid={:input('param.appid')}"
		,data:fieldData
		,config:{
			limit:30
			,height:evar.tableHeight
			,toolbar:true
			// ,skin:'line'
			,page:true
			,autoSort: false
			,method:'post'
			//详见layui文档 数据表格 - 基本参数
		}
		,checkbox:function(field,obj){
            if(obj.elem.checked){
	   			ajaxField('field='+  field.name +'&value=1&sid=' + field.value)
		   	}else{
		   		ajaxField('field='+  field.name +'&value=0&sid=' + field.value)
		   	}
        }
	})
 	
 	//监听是否展示
 	form.on('switch(SonApplist_is_display)', function(data){
 		if(data.elem.checked){
	  		ajaxField('field=is_display&value=1&sid=' + data.value);
	  	}else{
			ajaxField('field=is_display&value=0&sid=' + data.value);
	  	}
	});  

  	//监听单元格编辑
	getTable.on('edit', function(obj) {
		var value = obj.value //得到修改后的值
    	,data = obj.data //得到所在行所有键值
   	 	,field = obj.field; //得到字段
   	 	var update_data="sid="+data.sid+"&field="+field+"&value="+ value;
 		ajaxField(update_data)
	});
	
	//监听排序
	getTable.on('sort',function(obj){
		tableArg.order= obj.field + " " + obj.type;
		table.reload(getTable.id, {
		    initSort: obj //记录初始排序，如果不设的话，将无法标记表头的排序状态。
		    ,where: tableArg
		});
	})
  
	getTable.on('tool', function(obj) {
		var data = obj.data;
		//删除数据
		if(obj.event === 'SonApplist_del') {
			layer.confirm('确定此条删除吗？删除后不可恢复？', function(index) {
					layer.close(index);
					//删除
					var d_data = "sid="+data.sid;
					$.ajax({
							type: 'POST',
							url: "{:url("sonapplist",["t"=>3])}",
							data: d_data,
							success: function(data) {
								if(data.code == 1) {
									layer.msg('删除成功');
									obj.del();
								} else {
	
									layer.alert('删除失败，请刷新后重试');
								}
							}});
					});
		}else if(obj.event === 'url'){
	     	if(data.type==1){
	     		layer.msg('数据库模块类型不能编辑此项')
	     	}else{
	     		layer.prompt({
			         formType: 2
			         ,title: '修改 ID 为 [ '+ data.sid +' ] 的页面地址'
			         ,value: data.url
			       }, function(value, index){
			         layer.close(index);
			        var update_data="sid="+data.sid+"&field=url&value="+ value;
	   					ajaxField(update_data)
			         obj.update({
			           url: value
			         });
			       });
	     	}
       
		}else if(obj.event === 'type'){
			var d = {id:data.sid};
			laytpl($('#update-type-tpl').html()).render(d, function(html){
			  layer.open({
				  type: 1,
				  title: '修改 ID 为 [ '+ data.sid +' ] 的类型',
				  // skin: 'layui-layer-rim', //加上边框
				  area: ['300px', '200px'], //宽高
				  content: html,
				  success:function(){
				  	form.val("update-type-tpl-form", {
					  "type": data.type
					})
					form.render;
				  }
				});
			});
	     	
       
		}
		
//		if(obj.event === 'applist_power') {
//			location='/admin.php/son/s_admin/page/admin_sonapp_list?appid='+data.appid+'&appname='+data.name;
//		} 
	});

	form.on('select(update-type-tpl-form-type)', function(data){
	  
	  var id = $('.update-type-tpl-form-type').data('id')
	  ajaxField('field=type&value='+data.value+'&sid=' +id);
	  table.reload(getTable.id, {
		    // initSort: obj //记录初始排序，如果不设的话，将无法标记表头的排序状态。
		    where: tableArg
	  });
	});      

	function ajaxField(d_data){
		$.ajax({
			type: 'POST',
			url: "{:url("",["t"=>2])}",
			data: d_data,
			success: function(data) {
				if(data.code == 1) {
					layer.msg('修改成功');
					
				} else {
					var msg = data.msg?' 原因：'+data.msg:'';
					layer.alert('修改失败，请刷新后重试。'+msg);
				}
			}});
		
	}

});
</script>
